Flash Remoting Article

Introduction to Flash Remoting with .NET

Jason Michael Perry

www.jasonmperry.com

Flash Remoting works as a middleware that can connect a Flash interface with business logic developed in Coldfusion MX, Microsoft .NET, J2EE, XML, and web services. With Flash Remoting, you can synchronize Flash user interface elements with databases and other data stored on distributed systems. Flash Remoting also allows the Flash scripting language, ActionScript to manipulate or sort database results as a RecordSet object while transforming returned data types and objects to its native language. You can subsequently bind this data to user interface components in Flash MX Professional 2004.

This article will help you take the initial steps to developing Rich Internet Applications with ASP.NET. You will develop a Flash Remoting application that displays a personalized message using an ASP.NET page. You can view an example of the completed Flash application and experimental Flash Remoting applications at www.jasonmperry.com.

Requirements

This article explains how to connect Flash Applications to .NET with Flash Remoting using ASP.NET, assemblies, and ADO.NET. To complete this tutorial, intermediate knowledge of ActionScript and ASP.NET is recommended.

While this is a very simple tutorial, since it is a foundation for more complex work later, it requires the following software:

Flash MX Professional 2004

(Note: This tutorial also functions in Macromedia Flash MX.)

Flash Remoting for .NET


ASP .NET Development Kit (.NET Framework)


Microsoft IIS version 5 or later installed

 

Sample files:

(Note: These files are optional. You create all files within the tutorial, but use them as a reference if needed.)

Dreamweaver MX 2004


Flash Remoting Components for Flash MX Professional 2004

(Install these components after you install Flash MX Professional 2004.)



Overview of .NET with Flash Remoting

Flash Remoting adds to Flash MX Professional 2004's ability to read XML and web services with powerful options for connecting to .NET business logic: assemblies (DLLs), ASP.NET pages, and XML web services. In this article, I will explain the advantages and disadvantages of each implementation and develop an example application using an ASP.NET page.

A .NET assembly is a compiled DLL. Each DLL includes one or several classes, each with methods and properties. When Flash Remoting calls an assembly directly, it can call any of its methods directly and manipulate the returned data as native Flash objects. An advantage of an assembly is it allows a Flash interface to call any of its multiple methods and is much quicker than an XML web service.

ASP.NET pages provide features similar to those provided by an assembly, but instead of comprising multiple classes, methods, and properties, each ASP.NET page works as one complex method. When called by Flash Remoting, the ASP.NET page can access properties passed during the call and return data based on that information. An ASP.NET page is similar in speed to an assembly and quicker than a web service or XML.

Web services let Flash Remoting access several properties and methods over a distributed network. This solution is perfect for syndicating business logic over the web. The main problem with a web service is that you must send a request and response over the Internet. When using web services with Flash Remoting and .NET, your Flash Remoting application will dynamically create a compiled proxy assembly to communicate with the web service or XML file. For more information on XML and web services with .NET, read Consuming Amazon with Flash Remoting.

Connecting ASP.NET to Flash Remoting

For Flash Remoting to communicate with an ASP.NET page ensure that you have installed Flash Remoting on your web server machine. (See the Requirements section of this tutorial.) You will also need Internet Information Server and the .NET framework running on that machine. If everything is installed properly, Flash Remoting will create a sample directory named flashremoting on the web server. This example directory will house this article’s Flash application and ASP.NET pages.

To create a separate website or directory that uses Flash Remoting, open a new site with Internet Information Server and copy the contents of flashremoting/bin into the new website’s bin directory. This will copy the server control and registration information that Flash Remoting needs to work. Also note that Flash Remoting generates a Flash.log file when an error occurs. Viewing this log will provide a description of any connectivity errors you encounter to ease troubleshooting.

As a reference, here is a list of the files you will need to create in the flashremoting directory:

bin/flashremoting.dll
FlashRemotingService.aspx
gateway.aspx

To get started, open Dreamweaver MX 2004 or your preferred ASP.NET development environment and create a new file named FlashRemotingService.aspx. This ASP.NET page works as a remote service that you will later manipulate in Flash. In this article, I use C# for example, but you can recreate the code in VBScript or JavaScript. I also reference the Flash Remoting Server Control, an ASP.NET assembly that gives the ASP.NET page access to parameters and lets it return data to the Flash application.

<%@ Page Language="C#" debug="true" %>
<%@ Register TagPrefix="MM" Namespace="FlashGateway" Assembly="flashgateway" %>
<html>
<head>
  <title>jasonmperry.com  Flash Remoting Example</title>
</head>

<body bgcolor="#ffffff" text="#000000">
</body>
</html>

For ASP.NET developers, the above code appears to be basic except for the addition of one declaration, Register. This declaration registers the Flash Remoting server control flashgateway.dll for the ASP.NET page. Flash Remoting will use this gateway to get parameters from the Flash application and return any results back to the application.

Now that the server control is registered on the page, create a new instance of this control by adding the following tag to the HTML body tag:

<body bgcolor="#ffffff" text="#000000">
<MM:Flash id="Flash" runat="server" />
</body>
	

This tag, similar to other ASP.NET server controls, uses the ID to create a programmatic reference to the assembly. Notice the tag uses an MM prefix like the value you used in the Register declaration. This tag does not embed a Flash file into the page; it only creates an instance for use in Flash Remoting.

You can now access Flash parameters and pass a result to your Flash interface using the Flash Server Control. To do this, add the Page_Load callback function as follows:

<script language="C#" runat="server">
void Page_Load( Object sender, EventArgs e)
{
	string name = "";

	if( Flash.Params.Count > 0 )
	{
	  name = Flash.Params[0].ToString();
	}

	Flash.Result = "Hello" + name + "Welcome to Flash Remoting!";
}
</script>

This script determines if parameters were passed from the Flash interface. If so, it gets the first parameters as a string. It uses the string value to combine a more complex string to the returned value.

Manipulating ASP.NET with Flash

With the ASP.NET page completed, you can now create a Flash interface to call and manipulate the service.

To start, ensure that you have installed the Flash Remoting Components into Flash. Next, create a new document in Flash MX Professional 2004, select Frame 1 in the Timeline, and open the Actions panel. Here, you must include the NetServices package to implement the Flash Remoting connection and initialize a connection to the ASP.NET page. The NetServices ActionScript file includes several object definitions and methods to carry out the Flash Remoting connection. As an option, you can include a NetDebug ActionScript file to provide additional debugging for your documents. If you’re using Flash MX you may need to download the Flash Remoting Components. These components are necessary to use the external NetServices and NetDebug ActionScript files.

#include "NetServices.as"

if( inited == null )
{
	inited = true;
	NetServices.setDefaultGatewayUrl( "http://localhost/flashremoting/gateway.aspx" );
	gatewayConnection = NetServices.createGatewayConnection();
	
	ASPXService = gatewayConnection.getService( "flashremoting", this );
}

This code block creates a connection to the Flash Remoting server and creates a reference to the ASP.NET service. For this method to work, you must create a blank file named gateway.aspx. The URL to this file is passed to the setDefaultGateway. Once the gateway URL is passed, the createGatewayConnection method creates the Flash Remoting server connection. Flash Remoting with .NET requires that you create a physical gateway.aspx file, but other implementations of Flash Remoting do not require a physical gateway file. A best practice is to pass the default gateway as a parameter in the embed tags of your Flash file. This lets you move the Flash Remoting application to a new location without making physical changes to the Flash file. (Note: Upcoming changes in how Internet Explorer reads active content may require confirmation form a user before processing embed tags in Flash. You can find more information on these changes at Macromedia’s Active Content Developer Center.) This code block is packaged in an if statement to prevent Flash Remoting objects from being initialized more than once.

Continuing on in the code above, provide a path to the ASP.NET document. In the follwing line of code:

ASPXService = gatewayConnection.getService( "flashremoting", this );

you set the path to the Flash Remoting Service using dot notation. With ASP.NET pages, provide the directory from the Flash Remoting web server’s root using dot notation. For example, calling an ASP.NET page named http://localhost/flashremoting/cs/services/test.aspx will use the notation flashremoting.cs.services. This implementation will slightly differ for an XML web service or .NET assembly. In our document, the ASP.NET service is located at http://localhost/flashremoting/FlashRemotingService.aspx, making the dot notation for our service simply flashremoting.

With the reference of our .NET service complete, call the ASP.NET page and perform any operations on the page’s results. You need to create callback functions that process the result data or provide the status of the method if an error occurs. As a best practice, mask the call to the .NET page with an ActionScript function. This way you can add any last operations or error checking before the service call.

function FlashRemotingService( name )
{
	if( (name != null) && (name != "") )
	{
		ASPXService. FlashRemotingService ( name );
	}
}

function FlashRemotingService_Result( result ) 
{
}

function FlashRemotingService_Status( error )
{
	trace( error.code );
	trace( error.description );
	trace( error.details );
}

The function is the same name as the ASP.NET page. The callback functions described above are created by adding _Result and _Status to the ASP.NET page name. The Flash Player will automatically call the proper callback function when data is received from the ASP.NET service. The result data is automatically transformed to the corresponding ActionScript type for immediate use. Figure 1 shows a detailed list of what .NET types wrap to what Flash Remoting types.

ActionScript to .NET type transformation table
ActionScript Data Type .NET Data Type
Number Int, float, double
Boolean Bool
String String
Array Array
Object Object
Date Date

 

When the result callback function is called, the Flash application will dynamically produce a text field to display the result string. To ensure the entire text displays in the text field, set the autosize property to true. Edit the FlashRemotingService_Result function in the code example above as follows:

function FlashRemotingService _Result( result )
{
	if( txtResult == undefined )
	{
	    _root.createTextField( "txtResult", 100, 5.7, 87, 160, 18 );
	}
	txtResult.text = result;
	txtResult.autoSize = true;
}
  

Lastly, you must provide the a form and button to submit a custom message for the application user. To do this, create a text field with type set to input. The text field should have an instance name of txtInputName. Place a label using the text field static type next to the input box with the text name:.

Using the library create a button and place it next to the input text field. Add the following ActionScript to the button:

on( release )
{
	FlashRemotingService( txtInputName.text );
}

  

This ActionScript on the button will use the onRelease callback function above to submit the name data to the ASP.NET service. Once the ASP.NET page processes the operation, the Flash Player will use the result callback function to display the resulting string to the end user.


About the author

Jason Michael Perry is a partner in Out the Box Web Productions a web-consulting and training firm whose clients include Michael Jackson, Pan-American Life, and the Small Business Exchange. Jason is also a Macromedia Team Member and a Macromedia Certified Instructor. For more information on Jason visit www.outthebox.net or www.jasonmperry.com. You can also e-mail Jason with any questions or comments at Jason@jasonmperry.com.